﻿using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using MaxStoreErp.Core.BaseClasses;
using MaxStoreErp.Core.Commands;
using MaxStoreErp.Core.Interfaces;
using MaxStoreErp.Core.Threading;

namespace MaxStoreShop.Core.ViewerBase
{
    public class ViewerPresenterBase<TView, TViewModel, TItemType> : PresenterBase<TView, TViewModel>
        where TView : Control, IView, new()
        where TItemType : EntityBase, new()
        where TViewModel : ViewerViewModelBase<TItemType>, new()
    {
        protected override void OnViewModelSet()
        {
            base.OnViewModelSet();
            ViewModel.DeleteItemCommand = new RelayCommand(Delete, IsItemSelected);
            ViewModel.AddNewItemCommand = new RelayCommand(AddNewItem);
            ViewModel.EditSelectedItemCommand = new RelayCommand(EditSelectedItem);
        }

        async void Delete()
        {
            var selectedItemId = ViewModel.SelectedItem.Id;
            var result =
                await
                    UserMessage.QuestionMessageAsync(
                        string.Format("Вы действительно хотите удалить {0}?", ViewModel.SelectedItem),
                        "Удаление");
            if (result == MessageResult.No)
                return;

            var deletingItem = ViewModel.ViewerData.First(c => c.Id == selectedItemId);
            ThreadHelper.SafeCall(() =>
            {
                var cancel = new CancelEventArgs(false);
                OnDeleteItem(deletingItem, cancel);
                if (!cancel.Cancel)
                    RemoveItemFromViewerData(deletingItem);
            });
        }

        protected virtual void OnDeleteItem(TItemType removingItemType, CancelEventArgs cancelEventArgs)
        {

        }

        void RemoveItemFromViewerData(TItemType removingItemType)
        {
            ViewModel.ViewerData.Remove(removingItemType);
        }

        bool IsItemSelected()
        {
            return ViewModel.SelectedItem != null;
        }

        void AddNewItem()
        {
            ShowEditor(new TItemType());
        }
        void EditSelectedItem()
        {
            ShowEditor(ViewModel.SelectedItem);
        }
        protected virtual void ShowEditor(TItemType item)
        {

        }

        void ViewOnMouseDoubleClick(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            EditSelectedItem();
        }

        void ViewOnKeyDown(object sender, KeyEventArgs keyEventArgs)
        {
            switch (keyEventArgs.Key)
            {
                case Key.F2:
                    {
                        EditSelectedItem();
                        break;
                    }
                case Key.F3:
                    {
                        AddNewItem();
                        break;
                    }
                case Key.Delete:
                    {
                        Delete();
                        break;
                    }
                case Key.F5:
                    {
                        OnRefreshRequired();
                        break;
                    }
            }
        }

        protected virtual void OnRefreshRequired()
        {
            if (ViewModel.ReloadViewModelData != null && ViewModel.ReloadViewModelData.CanExecute())
                ViewModel.ReloadViewModelData.Execute();
        }

        public override void OnViewLoading()
        {
            base.OnViewLoading();
            if (ViewModel.TargetEditionElement != null)
            {
                ViewModel.TargetEditionElement.KeyDown += ViewOnKeyDown;
                ViewModel.TargetEditionElement.MouseDoubleClick += ViewOnMouseDoubleClick;
            }
        }

        public override void OnViewClosed()
        {
            base.OnViewClosed();
            if (ViewModel.TargetEditionElement != null)
            {
                ViewModel.TargetEditionElement.KeyDown -= ViewOnKeyDown;
                ViewModel.TargetEditionElement.MouseDoubleClick -= ViewOnMouseDoubleClick;
            }
        }
    }
}
